package com.zg.ktcomwan.utils

import android.app.Activity
import android.content.Context
import android.content.res.Configuration
import android.os.Build
import android.util.DisplayMetrics
import android.view.Display
import android.view.View
import android.view.WindowManager

/***
 * @Author:      zxd
 * @CreateDate:  2021/8/26 17:13
 * @Description:
 */
class ScreenUtils {

    //隐藏底部状态栏
    fun hideBottomUIMenu(context: Context) {
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            val v = (context as Activity).window.decorView
            v.systemUiVisibility = View.GONE
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            val decorView = (context as Activity).window.decorView
            val uiOptions = (View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
            decorView.systemUiVisibility = uiOptions
        }
    }

    /**
     * 是否有底部状态栏
     *
     * @param context
     * @return
     */
    fun checkDeviceHasNavigationBar(context: Context): Boolean {
        var hasNavigationBar = false
        val rs = context.resources
        val id = rs.getIdentifier("config_showNavigationBar", "bool", "android")
        if (id > 0) {
            hasNavigationBar = rs.getBoolean(id)
        }
        try {
            val systemPropertiesClass =
                Class.forName("android.os.SystemProperties")
            val m =
                systemPropertiesClass.getMethod("get", String::class.java)
            val navBarOverride =
                m.invoke(systemPropertiesClass, "qemu.hw.mainkeys") as String
            if ("1" == navBarOverride) {
                hasNavigationBar = false
            } else if ("0" == navBarOverride) {
                hasNavigationBar = true
            }
        } catch (e: Exception) {
        }
        return hasNavigationBar
    }

    fun getWight(mContext: Context): Int {
        val wm =
            mContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val display = wm.defaultDisplay
        var screenWidth = 0
        /*DisplayMetrics dm = new DisplayMetrics();
        ((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);*/
        //DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            val dm = DisplayMetrics()
            display.getRealMetrics(dm)
            screenWidth = dm.widthPixels
            //或者也可以使用getRealSize方法
//            Point size = new Point();
//            display.getRealSize(size);
//            screenHeight = size.y;
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            screenWidth = try {
                Display::class.java.getMethod("getRawWidth").invoke(display) as Int
            } catch (e: Exception) {
                val dm = DisplayMetrics()
                display.getMetrics(dm)
                dm.heightPixels
            }
        }
        return screenWidth
    }

    /**
     * 某些手机有底部导航栏 所以获取的高度不是真实的高度 而是去掉导航栏的高度，所有这里获取真实高度需要重新就算 by zxd
     *
     * @param mContext
     * @return
     */
    fun getHeight(mContext: Context): Int {
        val wm =
            mContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val display = wm.defaultDisplay
        var screenHeight = 0
        /*DisplayMetrics dm = new DisplayMetrics();
        ((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);*/
        //DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            val dm = DisplayMetrics()
            display.getRealMetrics(dm)
            screenHeight = dm.heightPixels
            //或者也可以使用getRealSize方法
//            Point size = new Point();
//            display.getRealSize(size);
//            screenHeight = size.y;
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            screenHeight = try {
                Display::class.java.getMethod("getRawHeight").invoke(display) as Int
            } catch (e: Exception) {
                val dm = DisplayMetrics()
                display.getMetrics(dm)
                dm.heightPixels
            }
        }
        return screenHeight
    }

    /**
     * 获取底部导航栏高度 by zxd
     *
     * @param mContext
     * @return
     */
    fun getNavigationBarHeight(mContext: Context): Int {
        var navigationBarHeight = -1
        val resources = mContext.resources
        val resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android")
        if (resourceId > 0) {
            navigationBarHeight = resources.getDimensionPixelSize(resourceId)
        }
        return navigationBarHeight
    }

    fun isInRight(mContext: Context, xWeight: Int): Boolean {
        return xWeight > getHeight(mContext) * 3 / 4
    }

    fun isInLeft(mContext: Context, xWeight: Int): Boolean {
        return xWeight < getWight(mContext) * 1 / 4
    }

    fun dip2px(context: Context, dipValue: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (dipValue * scale + 0.5f).toInt()
    }

    fun px2dip(context: Context, pxValue: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (pxValue / scale + 0.5f).toInt()
    }


    /**
     * 是否横屏
     *
     * @param mContext
     * @return
     */
    fun screenIsLanscape(mContext: Context): Boolean {
        var ret = false
        when (mContext.resources.configuration.orientation) {
            Configuration.ORIENTATION_PORTRAIT -> ret = false
            Configuration.ORIENTATION_LANDSCAPE -> ret = true
            else -> {
            }
        }
        return ret
    }

    /**
     * 获取当前屏幕状态
     *
     * @param mContext
     * @return
     */
    fun getOrientation(mContext: Context): Int {
        return mContext.resources.configuration.orientation
    }

    /**
     * 获取16：10图片的宽度。这个宽度实际就是屏幕的宽度-2dp /2.
     *
     * @param mContext 只用于两列的全屏listView 中
     * @return
     */
    fun getImageWidht16_10(mContext: Context): Int {
        return (getWight(mContext) - dip2px(mContext, 2f)) / 2
    }

    /**
     * 获取16：10图片的高度。这个方法和getImageWidht16_10() 有关。比例
     *
     * @param mContext
     * @return
     */
    fun getImageHight16_10(mContext: Context): Int {
        return (getImageWidht16_10(mContext) / 1.6).toInt()
    }

    /**
     * 根据屏幕宽度获取16-10的屏幕高度
     */
    fun getImageWidth16_10(heightPx: Int): Int {
        return (heightPx * 1.6).toInt()
    }

    fun getImageHeight16_10(widthPx: Int): Int {
        return (widthPx / 1.6).toInt()
    }

    fun getImageHeight16_9(widthPx: Int): Int {
        return widthPx * 9 / 16
    }

    fun getImageHeight7_2(widthPx: Int): Int {
        return widthPx * 2 / 7
    }
}